package com.yydream.controller;

import com.yydream.common.utils.R;
import com.yydream.common.valid.AddGroup;
import com.yydream.common.valid.UpdateGroup;
import com.yydream.dto.*;
import com.yydream.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "角色管理功能接口")
@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("/sysRole/detail")
    public R queryById(@RequestParam(value = "id") Long id){
        return sysRoleService.queryById(id);
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @PostMapping("/sysRole/list")
    public R list(@RequestBody SysRolePageDTO req){
        return sysRoleService.listPage(req);
    }

    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/sysRole/insert")
    public R add(@Validated(AddGroup.class) @RequestBody SysRoleDTO sysRole){
        return sysRoleService.insert(sysRole);
    }

    /**
     * 更新数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/sysRole/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody SysRoleDTO sysRole){
        return sysRoleService.update(sysRole);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/sysRole/deleteById")
    public R deleteById(@RequestParam(value = "ids", required = true) Long[] ids){
        return sysRoleService.deleteById(ids);
    }

    /**
     * 角色关联菜单
     *
     * @param roleRelationMenuDTO 实例对象
     * @return 实例对象
     */
    @ApiOperation("角色关联菜单")
    @PostMapping("/sysRole/roleRelationMenu")
    public R roleRelationMenu(@Valid @RequestBody RoleRelationMenuDTO roleRelationMenuDTO){
        return sysRoleService.roleRelationMenu(roleRelationMenuDTO);
    }

    /**
     * 查询角色已关联菜单
     *
     * @param roleId 角色id
     * @return 实例对象
     */
    @ApiOperation("查询角色已关联菜单")
    @GetMapping("/sysRole/roleRelationMenu/{roleId}")
    public R getRoleRelationMenu(@PathVariable(name = "roleId") Integer roleId){
        return sysRoleService.roleRelationMenuByRoleId(roleId);
    }

    /**
     * 角色关联用户
     *
     * @param roleRelationUserDTO 实例对象
     * @return 实例对象
     */
    @ApiOperation("角色关联用户")
    @PostMapping("/sysRole/roleRelationUser")
    public R roleRelationMenu(@Valid @RequestBody RoleRelationUserDTO roleRelationUserDTO){
        return sysRoleService.roleRelationUser(roleRelationUserDTO);
    }

    /**
     * 查询角色已关联用户
     *
     * @param roleId 角色id
     * @return 实例对象
     */
    @ApiOperation("查询角色已关联菜单")
    @GetMapping("/sysRole/roleRelationUser/{roleId}")
    public R getRoleRelationUser(@PathVariable(name = "roleId") Integer roleId){
        return sysRoleService.roleRelationUserByRoleId(roleId);
    }

}
